package com.pearl.generator.common;

/**
 * @author TangDan
 * @version 1.0
 * @since 2022/6/29
 */

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel(value = "Result对象", description = "统一结果集")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 4580737268023862568L;

    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "响应信息")
    private String msg;
    @ApiModelProperty(value = "数据")
    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public Result() {
    }

    public Result(Integer code) {
        this.code = code;
    }

    //是否成功（自定义结果码为1000为成功）
    @JsonIgnore
    public boolean isSuccess() {
        return this.code == 1000;
    }


    //成功时引用

    public static <T> Result<T> success() {
        return success(ResultEnum.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        return success(ResultEnum.SUCCESS, data);
    }

    public static <T> Result<T> success(ResultEnum re) {
        return success(re, null);
    }

    public static <T> Result<T> success(ResultEnum re, T data) {
        Integer code = re.getCode();
        String msg = re.getMsg();
        return success(code, msg, data);
    }

    public static <T> Result<T> success(Integer code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }


    //失败时引用

    public static <T> Result<T> fail() {
        return fail(ResultEnum.FAIL);
    }

    public static <T> Result<T> fail(ResultEnum re) {
        return fail(re, null);
    }

    public static <T> Result<T> fail(String msg) {
        return fail(ResultEnum.FAIL.getCode(), msg, null);
    }

    public static <T> Result<T> fail(T data) {
        return fail(ResultEnum.FAIL, data);
    }

    public static <T> Result<T> fail(ResultEnum re, T data) {
        Integer code = re.getCode();
        String msg = re.getMsg();
        return fail(code, msg, data);
    }

    public static <T> Result<T> fail(Integer code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}